package com.d3works.apps.hr.service.project;

import com.d3works.apps.hr.db.TimesheetLine;
import com.d3works.apps.hr.db.repo.TimesheetLineRepository;
import com.d3works.apps.project.db.Project;
import com.d3works.apps.project.service.ProjectActivityDashboardServiceImpl;
import com.d3works.mail.db.MailMessage;
import com.google.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ProjectActivityDashboardServiceHRImpl extends ProjectActivityDashboardServiceImpl {

  @Inject protected TimesheetLineRepository timesheetLineRepo;

  @Override
  protected String getActionLink(String model) {
    if (TimesheetLine.class.getName().equals(model)) {
      return "#/ds/project.spent.time/edit/";
    }
    return super.getActionLink(model);
  }

  @Override
  protected List<String> getRelatedModels() {
    List<String> relatedModelsList = super.getRelatedModels();
    relatedModelsList.add(TimesheetLine.class.getName());
    return relatedModelsList;
  }

  @Override
  protected Project getActivityProject(
      Project project, MailMessage message, Set<Long> projectIdSet) {
    if (TimesheetLine.class.getName().equals(message.getRelatedModel())) {
      TimesheetLine timesheetLine = timesheetLineRepo.find(message.getRelatedId());
      if (timesheetLine != null) {
        Project tsLineProject = timesheetLine.getProject();
        if (project == null
            || (tsLineProject != null && projectIdSet.contains(tsLineProject.getId()))) {
          return tsLineProject;
        }
      }
    }
    return super.getActivityProject(project, message, projectIdSet);
  }

  @Override
  protected Map<String, Object> getModelWithUtilityClass(String model) {
    Map<String, Object> dataMap = super.getModelWithUtilityClass(model);
    if (TimesheetLine.class.getName().equals(model)) {
      dataMap.put("modelName", "Timesheet line");
      dataMap.put("utilityClass", "label-important");
    }
    return dataMap;
  }
}
